<template>
  <section class="login-forget-main">
    <el-tabs stretch class="forget-inner" type="border-card">
      <el-tab-pane>
        <span slot="label">
          <i class="el-icon-mobile-phone" />手机号找回
        </span>
        <el-form ref="form" :model="form" :rules="rules" class="login-form forget" auto-complete="on" label-position="left" label-width="70px">
          <el-form-item prop="phoneNumber" label="手机号码:">
            <el-input v-model.trim="form.phoneNumber" auto-complete="on" name="phoneNumber" type="text" placeholder="请输入您的手机号码" />
          </el-form-item>
          <el-form-item prop="msgCode" label="验证码:">
            <el-input v-model.trim="form.code" style="width: 50%" name="code" auto-complete="new-password" placeholder="请输入验证码" />
            <el-button type="text" :disabled="disabled=!show" class="sendcode" @click="handleSendCode">
              <span v-show="show">获取验证码</span>
              <span v-show="!show" class="count">{{ count }}s后重新获取</span>
            </el-button>
          </el-form-item>
          <el-form-item prop="password" label="新密码:">
            <el-input v-model.trim="form.password" :type="pwdType" auto-complete="new-password" name="NewPassword" placeholder="请输入新密码" />
          </el-form-item>
          <el-form-item prop="confirmPassword" label="确认密码:">
            <el-input v-model.trim="form.confirmPassword" :type="pwdType" auto-complete="new-password" name="affirmPassword" placeholder="请输入确认密码" />
          </el-form-item>

          <el-button :loading="loading" type="primary" style="width:100%; height: 44px; margin-top: 20px;border-radius:20px;" @click.native.prevent="handleComit">确认</el-button>

          <el-button type="text" class="goback" @click="handleBack">返回登录</el-button>
        </el-form>
      </el-tab-pane>
    </el-tabs>
  </section>
</template>
<script>
import { phone } from '@/utils/validate'
import { getPhoneCode, submitPw } from '@/views/login/api/login'
import getAES from '../api/getAES'
export default {
  name: 'LoginForget',
  data() {
    return {
      loading: false,
      form: {
        phoneNumber: '',
        code: '',
        password: '',
        confirmPassword: ''
      },
      rules: {
        phoneNumber: [{ trigger: 'blur', validator: phone }],
        password: [{
          trigger: 'blur', validator: (rule, value, callback) => {
            // const reg = /^(?!.*\s)(?!^[\u4e00-\u9fa5]+$)(?!^[0-9]+$)(?!^[A-z]+$)(?!^[^A-z0-9]+$)^.{8,30}$/
            const reg = /^(?![a-zA-Z]+$)(?![A-Z0-9]+$)(?![A-Z\W_]+$)(?![a-z0-9]+$)(?![a-z\W_]+$)(?![0-9\W_]+$)[a-zA-Z0-9\W_]{8,30}$/
            if (!value) {
              callback(new Error('请输入新密码'))
            } else if (!reg.test(value)) {
              callback(new Error('密码长度必须大于等于8位小于等于30位。必须包含大写字母、小写字母、数字、特殊字符中的3种。'))
            } else if (this.form.confirmPassword && value !== this.form.confirmPassword) {
              callback(new Error('两次输入密码不一致!'))
            } else {
              callback()
            }
          }
        }],
        confirmPassword: [{
          trigger: 'blur', validator: (rule, value, callback) => {
            if (value === '') {
              callback(new Error('请再次输入密码'))
            } else if (value !== this.form.password) {
              callback(new Error('两次输入密码不一致!'))
            } else {
              callback()
            }
          }
        }],
        code: [{ message: '请输入验证码', trigger: 'blur' }]
      },
      TIME_COUNT: 60,
      count: 0,
      show: true,
      pwdType: 'password',
      timer: null
    }
  },
  created() {

  },
  beforeDestroy() {
    clearInterval(this.timer)
    this.timer = null
  },
  methods: {
    handleSendCode() {
      const reg = /^1\d{10}$/
      if (this.form.phoneNumber === '') {
        this.$message({
          message: '请输入手机号码！',
          type: 'warning'
        })
        return
      } else if (!reg.test(this.form.phoneNumber)) {
        this.$message({
          message: '请输入正确的11位手机号码！',
          type: 'warning'
        })
        return
      }
      if (!this.timer) {
        this.count = this.TIME_COUNT
        this.show = false
        return new Promise((resolve, reject) => {
          this.timer = setInterval(() => {
            if (this.count > 0 && this.count <= this.TIME_COUNT) {
              this.count--
            } else {
              this.show = true
              clearInterval(this.timer) // 清除定时器
              this.timer = null
            }
          }, 1000)
          getPhoneCode({
            phoneNumber: this.form.phoneNumber,
            smsType: '0'
          }).then(res => {
            this.$message({
              message: '短信已发送，请注意查收！',
              type: 'success'
            })
          }).catch(error => {
            reject(error)
            this.show = true
            this.timer = null
          })
        })
      }
    },

    handleBack() {
      this.$emit('closeforget')
    },

    handleComit() {
      this.$refs.form.validate(valid => {
        if (valid) {
          const formData = {
            phoneNumber: this.form.phoneNumber,
            smsCode: this.form.code,
            password: this.form.password,
            confirmPassword: this.form.confirmPassword,
            KEY: getAES
          }
          return new Promise((resolve, reject) => {
            submitPw(formData)
              .then(() => {
                this.$message({
                  title: '成功',
                  message: '修改成功',
                  type: 'success',
                  duration: 2000,
                  onClose: () => {
                    this.$emit('closeforget')
                  }
                })
              })
              .catch(error => {
                reject(error)
              })
          })
        } else {
          return false
        }
      })
    }
  }
}
</script>
<style lang="scss">
.login-forget-main {
  width: 100%;
  height: 100%;
  position: relative;
  .forget-inner {
    width: 460px;
    position: absolute;
    left: 0;
    right: 0;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    .el-button.sendcode {
      border: none;
      position: absolute;
      right: 0;
      bottom: 0px;
      font-size: 12px;
      padding: 0;
      color: $mainColor;
    }
    .el-button.goback {
      width: 100%;
      margin-left: 0;
    }
  }
}
</style>
